package com.nuumoblie.bluetoothtools.ble

import android.content.Intent
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import com.nuumoblie.bluetoothtools.BaseActivity
import com.nuumoblie.bluetoothtools.R
import com.nuumoblie.bluetoothtools.ble.db.dao.OnLoadComplete
import com.nuumoblie.bluetoothtools.ble.db.entity.BleAdvertiseInfo
import com.nuumoblie.bluetoothtools.ble.db.model.ProductTypeViewModel

class BlePeripheralListActivity : BaseActivity() {

    var mRecyclerView: RecyclerView? = null;
    var mPeripheralAdapter: PeripheralAdapter? = null;

    var mProductTypeViewModel: ProductTypeViewModel? = null;

    override fun getContentLayoutId(): Int {
        return R.layout.activity_peripheral_list;
    }

    override fun initToolbar() {
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
    }

    override fun initView() {
        mRecyclerView = findViewById(R.id.recyclerview);

    }

    override fun initData() {
        mPeripheralAdapter = PeripheralAdapter(this);
        mRecyclerView?.let {
            it.adapter = mPeripheralAdapter;
            it.layoutManager = LinearLayoutManager(this);
        }


        //        InitDatabase.initDatabase(this);
        mProductTypeViewModel = ViewModelProviders.of(this).get(ProductTypeViewModel::class.java);

//        mProductTypeViewModel?.allProducts?.listIterator()?.forEach {
//            Log.v("lmjssjj",it._id.toString());
//        }
        mProductTypeViewModel?.getAdvertiseInfo(object : OnLoadComplete {
            override fun onComplete(infos: List<BleAdvertiseInfo>?) {
                infos?.let {
                    mPeripheralAdapter?.setData(infos);
                }
            }
        });
    }

    override fun initEvent() {
        val fab: FloatingActionButton = findViewById(R.id.fab)
        fab.setOnClickListener { view ->
            //            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                .setAction("Action", null).show()
            startActivity(Intent(this, EditPeripheralActivity::class.java));
        }
    }
}